﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ESOL.Business.Sales;
using ESOL.Common;
using ESOL.Common.Entity;
using ESOL.WinUI.Hr;

namespace ESOL.WinUI.Sales
{
    public partial class frmMaintainSalesOrderQuery : BaseForm.frmBaseDialogyQueryForm
    {
        #region Variable
        public bool IsOnlyApproved { set; get; }
        private SalesOrderEntity mSelectedSalesOrder;
        public SalesOrderEntity SelectedSalesOrder
        {
            get { return mSelectedSalesOrder; }
        }
        public List<SalesOrderEntity> SelectedSalesOrderList
        {
            get;
            set;
        }
        private bool isMulti = false;
        #endregion

        #region Constructor
        public frmMaintainSalesOrderQuery()
        {
            InitializeComponent();
        }
        public frmMaintainSalesOrderQuery(bool multi)
        {
            InitializeComponent();
            gridColumn10.Visible = true;
            isMulti = multi;
        }
        #endregion

        #region Functions
        private void BindComboBox()
        {
            UICommon.UICommonFunctions.BindComboboxByCommonItem(cmbStatus, "SALES", true);
            cmbStatus.SetSelectedValue(SalesStatus.SALES03.ToString());

            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(colCURRENCY, "CS", true);
            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(colSTATUS, "SALES", true);
        }

        private void SetField()
        {
            if (IsOnlyApproved)
            {
                cmbStatus.Enabled = false;
            }
        }

        private string GetCondition()
        {
            string condition = "";
            if (txtCustomer.Tag != null && txtCustomer.Tag.GetType() == typeof(MstCustomerEntity))
            {
                MstCustomerEntity customer = (MstCustomerEntity)txtCustomer.Tag;
                condition += " AND SO.CUSTOMER_ID = '" + customer.CustomerId + "' ";
            }
            else
            {
                condition += @" AND SO.CUSTOMER_ID IN (SELECT CUSTOMER_ID FROM MST_PERSON_CUSTOMER PC
						LEFT JOIN MST_PERSON P ON P.PERSON_ID = PC.PERSON_ID
                        LEFT JOIN MST_PERSON P2 ON P2.PERSON_ID = P.SUPERVISOR
						LEFT JOIN MST_PERSON P3 ON P3.PERSON_ID = P2.SUPERVISOR
						LEFT JOIN MST_PERSON P4 ON P4.PERSON_ID = P3.SUPERVISOR
						LEFT JOIN MST_PERSON P5 ON P5.PERSON_ID = P4.SUPERVISOR
                        WHERE PC.PERSON_ID = '" + CommonParameter.CurrentUser.PersonId + @"'
	                    OR P2.PERSON_ID = '" + CommonParameter.CurrentUser.PersonId + @"'
	                    OR P3.PERSON_ID = '" + CommonParameter.CurrentUser.PersonId + @"'
	                    OR P4.PERSON_ID = '" + CommonParameter.CurrentUser.PersonId + @"'
	                    OR P5.PERSON_ID = '" + CommonParameter.CurrentUser.PersonId + @"')";
            }
            if (string.IsNullOrEmpty(txtQuoteCode.Text) == false)
            {
                condition += " AND SO.ORDER_CODE LIKE '%" + txtQuoteCode.Text + "%'";
            }
            if (txtDepartment.Tag != null && txtDepartment.Tag.GetType() == typeof(MstDepartmentEntity))
            {
                MstDepartmentEntity entity = (MstDepartmentEntity)txtCustomer.Tag;
                condition += " AND SO.ORDER_DEPARTMENT = '" + entity.DepartmentId + "' ";
            }
            if (txtOrderPerson.Tag != null && txtOrderPerson.Tag.GetType() == typeof(MstPersonEntity))
            {
                MstPersonEntity entity = (MstPersonEntity)txtOrderPerson.Tag;
                condition += " AND SO.ORDER_PERSON = '" + entity.PersonId + "' ";
            }
            if (string.IsNullOrEmpty(cmbStatus.Text) == false && cmbStatus.SelectedValue != null)
            {
                condition += " AND SO.STATUS = '" + cmbStatus.SelectedValue.ToString() + "'";
            }
            if (dtpOrderDate.EditValue!=null)
            {
                condition += " AND SO.ORDER_DATE >= CONVERT(DATETIME,'" + ((DateTime)dtpOrderDate.EditValue).ToString("yyyy-MM-dd") + "')";
            }
            if (dtpOrderDateTo.EditValue!=null)
            {
                condition += " AND SO.ORDER_DATE <= CONVERT(DATETIME,'" + ((DateTime)dtpOrderDateTo.EditValue).ToString("yyyy-MM-dd") + "')";
            }
            return condition;
        }

        private void BindDataGrid()
        {
            string condition = GetCondition();
            string orderBy = "ORDER_CODE DESC";
            DataTable dt = SalesOrderBs.GetByCondition(condition, orderBy, "");
            dt.Columns.Add("IS_SELECTED", typeof(Boolean));
            foreach (DataRow dr in dt.Rows)
            {
                dr["IS_SELECTED"] = false;
            }
            gvMain.DataSource = dt;
        }
        #endregion

        #region Event
        protected override void OnShown(EventArgs e)
        {
            BindComboBox();
            SetField();
            BindDataGrid();
            base.OnShown(e);
        }

        protected override void Click_Query()
        {
            BindDataGrid();
        }

        protected override void Click_Confirm()
        {
            if (isMulti == false)
            {
                if (mSelectedSalesOrder == null)
                {
                    MessageBox.Show("请选择一个销售订单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
            }
            else
            {
                DataTable dt = gvMain.DataSource as DataTable;
                SelectedSalesOrderList = new List<SalesOrderEntity>();
                SalesOrderEntity entity;
                foreach (DataRow dr in dt.Rows)
                {
                    if (Convert.ToBoolean(dr["IS_SELECTED"]))
                    {
                        entity = new SalesOrderEntity(dr);
                        SelectedSalesOrderList.Add(entity);
                    }
                }
                if (SelectedSalesOrderList == null || SelectedSalesOrderList.Count == 0)
                {
                    MessageBox.Show("请至少选择一个销售合同单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
            }
            this.DialogResult = DialogResult.OK;
        }

        private void txtCustomer_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainCustomerQuery frm = new frmMaintainCustomerQuery();
            if (frm.ShowDialog() == DialogResult.OK && frm.SelectedCustomer != null)
            {
                txtCustomer.Text = frm.SelectedCustomer.CustomerName;
                txtCustomer.Tag = frm.SelectedCustomer;
            }
            else
            {
                txtCustomer.Text = "";
                txtCustomer.Tag = null;
            }
        }

        private void txtDepartment_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainDepartmentQuery frm = new frmMaintainDepartmentQuery();
            if (frm.ShowDialog() == DialogResult.OK && frm.SelectedDepartment != null)
            {
                txtDepartment.Text = frm.SelectedDepartment.DepartmentName;
                txtDepartment.Tag = frm.SelectedDepartment;
            }
            else
            {
                txtDepartment.Text = "";
                txtDepartment.Tag = null;
            }
        }

        private void txtOrderPerson_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainEmployeeQuery frm = new frmMaintainEmployeeQuery();
            if (frm.ShowDialog() == DialogResult.OK && frm.SelectedPerson != null)
            {
                txtOrderPerson.Text = frm.SelectedPerson.NameCn;
                txtOrderPerson.Tag = frm.SelectedPerson;
            }
            else
            {
                txtOrderPerson.Text = "";
                txtOrderPerson.Tag = null;
            }
        }

        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            DataRow dr = gridView1.GetDataRow(e.RowHandle);
            mSelectedSalesOrder = new SalesOrderEntity(dr);
        }

        private void gridView1_DoubleClick(object sender, EventArgs e)
        {
            if (mSelectedSalesOrder == null)
            {
                MessageBox.Show("请选择一个销售订单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            this.DialogResult = DialogResult.OK;
        }

        #endregion
    }
}
